package com.ruoyi.websocket.controller;
import com.corundumstudio.socketio.AckCallback;
import com.corundumstudio.socketio.BroadcastAckCallback;
import com.corundumstudio.socketio.SocketIOServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author qiaofeng
 */
@RestController
@Slf4j
@RequestMapping("")
@CrossOrigin(origins = "*",maxAge = 3600)
public class IndexController {

    @Autowired
    SocketIOServer socketIOServer;


    @RequestMapping(value = "/msg")
    public void contextLoads(String msg) {
//        getRoomOperations("9999") 获取房间号为9999的所有客户端连接 var socket = io.connect("http://127.0.0.1:19099?openid=22323&appid=9999");
        socketIOServer.getRoomOperations("9999").sendEvent("message", msg, new AckCallback<String>(String.class, 1000) {
//            onSuccess 客户端成功接收并返回确认时触发
//            默认超时时间为1000毫秒（1秒）
            //AckCallback 提供异步确认回调机制，当客户端收到消息后会返回确认
            @Override
            public void onSuccess(String result) {
                log.info("反馈信息" + result);
            }
        });
    }


    @RequestMapping("/hello")
    public String hello(@RequestParam(name="name") String name,Model model){
        model.addAttribute("name",name);
        return "test";
    }

}